package com.zd.commodity.categoryMng.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.brandMng.entity.BrandShow;
import com.zd.commodity.categoryMng.biz.ICategoryBiz;
import com.zd.commodity.categoryMng.entity.Category;
import com.zd.commodity.userMng.entity.User;
@Scope("prototype")
@Controller("categoryAction")
public class CategoryAction extends BaseAction<Category>{
	
	private ICategoryBiz categoryBiz;
	private Category category;
	
	//添加子节点
	public void addChildCategory(){
		Category childCategory = new Category();
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		childCategory.setCreateTime(timestamp);
		childCategory.setUpdateTime(timestamp);
		childCategory.setUpdater(((User)request.getSession().getAttribute("user")).getId());
		childCategory.setParentId(category.getId());
		childCategory.setLevel(category.getLevel()+1);
		childCategory.setName(category.getName());
		try {
			categoryBiz.add(childCategory);
			write(ajaxReturn(true,"分类添加成功！",null));
		} catch (Exception e) {
			write(ajaxReturn(false,"分类添加失败！",null));
			e.printStackTrace();
		}
	}
	//修改节点名称
	public void updateCategory(){
		Category selfCategory = categoryBiz.get(category.getId());
		selfCategory.setName(category.getName());
		selfCategory.setUpdater(((User)request.getSession().getAttribute("user")).getId());
		selfCategory.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		try {
			categoryBiz.update(selfCategory);
			write(ajaxReturn(true,"分类名称修改成功！",null));
		} catch (Exception e) {
			write(ajaxReturn(false,"分类名称修改失败！",null));
			e.printStackTrace();
		}
	}
	
	//生成类别树形结构数据
	public void getCategoryTree() throws IOException{
		Category category = categoryBiz.get(super.getId());
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Category.class, "text", "href","nodes","state","tags");
		String jsonString = JSON.toJSONString(category,filter);
		super.responseWriter(jsonString);
	}
	
	//跳转时存放参数用
	private String categoryId;
	private String categoryName;
	public String getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
	public String getCategoryName() {
		return categoryName;
	}
	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	
	
	/**
	 * 添加类别和品牌的关联
	 */
	public String saveCategoryBrand(){
		String brandShowId=request.getParameter("brandShowId");
		Category category=categoryBiz.get(super.getT().getId());
		
		BrandShow brand=new BrandShow();
		brand.setId(Long.parseLong(brandShowId));
		
		List<BrandShow> list=category.getBrandShows();
		list.add(brand);
		
		category.setBrandShows(list);
		
		categoryBiz.update(category);	
		return "tofindBrands";
	}
	
	/**
	 * 删除类别和品牌的关联
	 */
	public String deleteCategoryBrand(){
		String brandId=request.getParameter("brandId");
		
		categoryBiz.deleteCategoryBrand(Long.valueOf(categoryId),Long.valueOf(brandId));
		return "tofindCategoryBrand";
	}
	
	/**
	 * 查询类别和品牌是否已存在
	 */
	public void checkRepeat(){
		String brandShowId=request.getParameter("brandShowId");
		HashMap<String, String> map=new HashMap<String, String>();
		map.put("brandShowId", brandShowId);
		map.put("categoryId", categoryId);
		Long size = categoryBiz.getPagedListCountBySql("checkCategoryBrandShowExistCount",map);
		if(size>0){
			write(ajaxReturn(false,"该类别下的品牌已存在，请重新选择",null));
		}else{
			write(ajaxReturn(true,"可以添加",null));
		}
	}
	

	public ICategoryBiz getCategoryBiz() {
		return categoryBiz;
	}
	@Autowired
	public void setCategoryBiz(ICategoryBiz categoryBiz) {
		this.categoryBiz = categoryBiz;
		this.setBaseBiz(categoryBiz);
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	
}
